package com.company.genealogy.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.genealogy.web.entity.Role;
import com.company.genealogy.web.entity.User;
import com.company.genealogy.web.mapper.RoleMapper;
import com.company.genealogy.web.mapper.UserRoleMapper;
import com.company.genealogy.web.service.RoleService;
import com.company.genealogy.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private UserService userService;
    
    @Override
    public List<Role> getEnabledRoles() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByAsc("id");
        return list(queryWrapper);
    }
    
    @Override
    public List<Role> getRolesByUserId(Long userId) {
        List<Long> roleIds = userRoleMapper.selectRoleIdsByUserId(userId);
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        return listByIds(roleIds);
    }

    @Override
    public List<Role> getRolesByUsername(String username) {
        User user = userService.getByUsername(username);
        if (user == null) {
            return new ArrayList<>();
        }
        return getRolesByUserId(user.getId());
    }
} 